/*
 * @copyright 2010 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of MyTricTrac.
 *
 * MyTricTrac is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MyTricTrac is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with My Accounts.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiprion.montrictracv4.db.dao;

import java.util.ArrayList;
import java.util.List;

import org.amphiprion.montrictracv4.ApplicationConstants.DATA_TYPE;
import org.amphiprion.montrictracv4.db.model.UserSearchData;

import android.content.Context;
import android.database.Cursor;

/**
 * This class is responsible of all database collection access.
 * 
 * @author amphiprion
 * 
 */
public class UserSearchMecanismDao extends AbstractDao {
	/** The singleton. */
	private static UserSearchMecanismDao instance;

	/**
	 * Hidden constructor.
	 * 
	 * @param context
	 *            the application context
	 */
	private UserSearchMecanismDao(Context context) {
		super(context);
	}

	/**
	 * Return the singleton.
	 * 
	 * @param context
	 *            the application context
	 * @return the singleton
	 */
	public static UserSearchMecanismDao getInstance(Context context) {
		if (instance == null) {
			instance = new UserSearchMecanismDao(context);
		}
		return instance;
	}

	public void persit(UserSearchData data) {
		String sql = "insert into USER_MECANISM_SEARCH (id_search, id_user_mecanism, min_value, max_value, value) values (?,?,?,?,?)";
		Object[] params = new Object[5];
		params[0] = data.searchId;
		params[1] = data.dataId;
		params[2] = data.min_value;
		params[3] = data.max_value;
		params[4] = data.txt_value;

		execSQL(sql, params);

	}

	public List<UserSearchData> getFromSearch(String searchId) {
		String sql = "SELECT usd.id_search, u.id, usd.min_value, usd.max_value, usd.value, u.data_type, u.name from USER_MECANISM_SEARCH usd, USER_MECANISM u where usd.id_search=? and usd.id_user_mecanism=u.id";
		Cursor cursor = getDatabase().rawQuery(sql, new String[] { searchId });
		List<UserSearchData> result = new ArrayList<UserSearchData>();

		if (cursor.moveToFirst()) {
			do {
				UserSearchData usd = new UserSearchData(cursor.getString(0), cursor.getString(1));
				usd.min_value = cursor.getInt(2);
				usd.max_value = cursor.getInt(3);
				usd.txt_value = cursor.getString(4);
				usd.typeForData = DATA_TYPE.values()[cursor.getInt(5)];
				usd.nameFromData = cursor.getString(6);
				result.add(usd);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return result;
	}
}
